import os

from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
CUSTOMER = 'C'
SHOP = 'S'
RIDER = 'R'


def get_avatar_name(instance, filename):
    extension = filename.split(".")[-1]
    # 生成新的文件名，例如：'my_file_1.jpg'
    new_filename = f'{instance.id}_avatar.{extension}'
    # 返回新的文件名
    return os.path.join('avatars', new_filename)


class User(AbstractUser):
    USER_TYPE_CHOICES = (  # 三种用户角色：顾客，商家和骑手
        (CUSTOMER, '顾客'),
        (SHOP, '商家'),
        (RIDER, '骑手'),
    )
    user_type = models.CharField(verbose_name='用户类型', max_length=3, choices=USER_TYPE_CHOICES, null=False)
    question = models.CharField(verbose_name='安全问题', max_length=255, null=False)
    answer = models.CharField(verbose_name='安全问题答案', max_length=255, null=False)
    phone_num = models.CharField(verbose_name='电话号码', max_length=12, null=False)  # 用于联系的电话号码
    avatar = models.ImageField(upload_to=get_avatar_name, null=True)


class CustomerProfile(models.Model):
    """
    顾客的账户信息
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=False, related_name='customer_profile')
    nickname = models.CharField(verbose_name='用户昵称', max_length=32, null=False)
    customer_add = models.CharField(verbose_name='用户常用地址', max_length=64, null=True)


class ShopProfile(models.Model):
    """
    商家的账户信息
    """
    SHOP_TYPE_CHOICES = (
        ('FOOD', '食品店'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=False, related_name='shop_profile')
    shop_name = models.CharField(verbose_name='商家名', max_length=64, null=False)
    shop_add = models.CharField(verbose_name='商家地址', max_length=64, null=False)
    shop_type = models.CharField(verbose_name='商家类型', max_length=10, choices=SHOP_TYPE_CHOICES)
    info = models.TextField(verbose_name='商家信息', null=True)
    is_working = models.BooleanField(verbose_name='营业状态', null=False, default=False)  # 为真则营业中，反之则打烊


class RiderProfile(models.Model):
    """
    骑手的用户信息
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=False, related_name='rider_profile')
    rider_name = models.CharField(verbose_name='骑手姓名', max_length=32, null=False)
    is_working = models.BooleanField(verbose_name='工作状态', null=False, default=False)  # 为真则工作中，反之则休息

